package com.cal.utms.model.maintenance;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import com.cal.utms.dao.maintenance.SystemConfigurationDaoImpl;
import com.cal.utms.dto.maintenance.SystemConfigurationDto;
import com.cal.utms.entity.ComplianceRecipientsEmail;
import com.cal.utms.entity.SystemConfiguration;

public class ConfigurationModel {

	SystemConfigurationDaoImpl configurationDaoImpl = new SystemConfigurationDaoImpl();

	public void save(SystemConfigurationDto configurationDto, String emails) {

		List<ComplianceRecipientsEmail> complianceRecipientsEmails = new ArrayList<ComplianceRecipientsEmail>();
		if (emails != null) {
			String[] string_email = emails.split(",");
			for (int i = 0; i < string_email.length; i++) {
				ComplianceRecipientsEmail email = new ComplianceRecipientsEmail();
				email.setEmail(string_email[i].replaceAll("^\"|\"$", ""));
				complianceRecipientsEmails.add(email);
			}
		}

		List<SystemConfiguration> configurations = new ArrayList<SystemConfiguration>();
		Field[] fields = configurationDto.getClass().getDeclaredFields();
		for (Field field : fields) {
			field.setAccessible(true);
			if (field.getName() != "configurationDtos" && field.getName() != "success" && field.getName() != "RestMsg" && field.getName() != "emails") {
				SystemConfiguration configuration = new SystemConfiguration();
				try {
					configuration.setPropertyName(field.getName());
					configuration.setValue((String) field.get(configurationDto));
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
				configurations.add(configuration);
			}
		}
		Object object = configurationDaoImpl.save(configurations, complianceRecipientsEmails);
		if (object instanceof String) {
			configurationDto.setSuccess(true);
			configurationDto.setRestMsg("SUCCESS");
		} else {
			configurationDto.setSuccess(false);
			configurationDto.setRestMsg("There is some error returned from the server.");
		}

	}

	public List<SystemConfigurationDto> getConfiguration() {
		List<SystemConfigurationDto> dtos = new ArrayList<SystemConfigurationDto>();
		SystemConfigurationDto configurationDto = new SystemConfigurationDto();
		List<SystemConfiguration> list = configurationDaoImpl.getAllConfig();
		for (SystemConfiguration systemConfiguration : list) {
			try {
				Field field = configurationDto.getClass().getDeclaredField(systemConfiguration.getPropertyName());
				field.setAccessible(true);
				try {
					field.set(configurationDto, systemConfiguration.getValue());
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			}
		}
		dtos.add(configurationDto);
		return dtos;
	}

	public List<ComplianceRecipientsEmail> getEmail() {
		List<ComplianceRecipientsEmail> emails = configurationDaoImpl.getAllComEmail();
		return emails;
	}
}
